﻿//-----------------------------------------------------------------------
// <copyright file="SQLDataProvider.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Collections.Generic;
using System.Data.EntityClient;
using FastNET.CleanAct.Client.ManagerEngineData.Repositories;
using FastNET.CleanAct.Common.Repository.Configuration;
using FastNET.CleanAct.Common.Repository.Interfaces.Repositories.Base;
using FastNET.CleanAct.Common.Repository.Interfaces.Repositories.General;
using FastNET.CleanAct.Common.Repository.Interfaces.Repositories.ManagementEngine;

#endregion

namespace FastNET.CleanAct.Client.ManagerEngineData
{
    /// <summary>
    /// Linq based SQLDataProvider for managing repositories and datasources
    /// </summary>
    public class SQLDataProvider : RepositoryProviderBase
    {
        #region Fields

        private static readonly Dictionary<Type, IRepository> _repositories;
        private static EntityConnection _connection;

        #endregion

        #region CTor/DTor

        /// <summary>
        /// Initializes static members of the SQLDataProvider class
        /// </summary>
        static SQLDataProvider()
        {
            // Initialize variables
            _repositories = new Dictionary<Type, IRepository>();

            // Create connection
            CreateConnection();

            // Build Repository Library
            _repositories.Add(typeof(IPackageRepository), new PackageRepository(_connection));
            _repositories.Add(typeof(IActionRepository), new ActionRepository(_connection));
            _repositories.Add(typeof(ILogEntryRepository), new LogEntryRepository(_connection));
            _repositories.Add(typeof(IScheduleRepository), new ScheduleRepository(_connection));
            _repositories.Add(typeof(IScheduleHistoryRepository), new ScheduleHistoryRepository(_connection));
            _repositories.Add(typeof(ITemplateRepository), new TemplateRepository(_connection));
            _repositories.Add(typeof(ITemplateGroupRepository), new TemplateGroupRepository(_connection));
        }

        #endregion

        #region Retrievals

        /// <summary>
        /// Gets the requested repository based on the TypeParam
        /// </summary>
        /// <typeparam name="T">RepositoryType to retrieve</typeparam>
        /// <returns>IRepositoryBase with the requested repository or null if none was found</returns>
        public override T GetRepository<T>()
        {
            if (_repositories.ContainsKey(typeof(T)))
            {
                return (T)_repositories[typeof(T)];
            }

            throw new InvalidOperationException(string.Format("The repository of type {0} is unsupported by this provider", typeof(T)));
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Creates the general connection for connecting to the data store
        /// </summary>
        private static void CreateConnection()
        {
            EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder
                {
                    Metadata = "res://*/Entities.ManagerEngineModel.csdl|res://*/Entities.ManagerEngineModel.ssdl|res://*/Entities.ManagerEngineModel.msl",
                    Provider = "System.Data.SqlServerCe.3.5",
                    ProviderConnectionString = Properties.Settings.Default.ManagerEngineConnectionString
                };

            _connection = new EntityConnection(builder.ConnectionString);

            _connection.Open();
        }

        #endregion
    }
}